#!/usr/bin/perl -w
# 
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
#
# -- 07/06/2009 Created by Zhixin Li
# -- Used to collect mysql configuration file and mysql datadir permission 
# -- output  value|value means  datadir_mode|cf_mode

use strict;

my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Status${DELIM}Down\n";
    exit 1;
}
my $SQL1="show variables like 'pid_file'";
my $SQL2="show variables like 'datadir'";
my $file=`$MYSQL -N -s $PARAM --batch -e "$SQL1"`;
my $dir=`$MYSQL -N -s $PARAM --batch -e "$SQL2"`;
my $pidfile = substr($file,index($file,"\t")+1,length($file)-index($file,"\t")+1);
my $datadir = substr($dir,index($dir,"\t")+1,length($dir)-index($dir,"\t")+1);
my $ostype = `/bin/uname`;
my $mypid;
my $proc;
chomp($pidfile);
chomp($datadir);
open (FILE, "$pidfile") or die "Couldn't open $pidfile for reading: $!\n";
$/=""; 
$mypid = <FILE>; 
close(FILE); 
$/="\n";
chomp($mypid);
chomp($ostype);
my $cf_mode;          #configuration file permission mode 
my $x;
my $y;
my @z;
if ($ostype eq 'SunOS') {
   $proc =`/usr/ucb/ps awwx|/bin/grep -v grep|/bin/grep mysqld|/bin/grep -w "$mypid "`; 
   $x = substr($proc,index($proc,"/"),length($proc));              # get process command
   $y = grep(/--defaults-file/,$x);                                # check if mysqld start with --defaults-file option
   if ( $y == 0) {                                                 # if no defaults-file then check /etc/my.cnf 
      if (-e '/etc/my.cnf') {
         $cf_mode = (stat('/etc/my.cnf'))[2];
         chomp($cf_mode);
      } else {                                                     # if no /etc/my.cnf then report 0000
         $cf_mode=00000;
      }
   }else {
      @z = split (/ +/, $x);
      foreach (@z) {
         if (/^--defaults-file/) {
            chomp($_);
            my @fields = split(/=/,$_);
            my $cffile = $fields[1];
            if (-e $cffile) {
               $cf_mode = (stat($cffile))[2];
               chomp($cf_mode);
            } else {
              $cf_mode=00000;
            }
         }
      }
   }
} else {
   $proc =`/bin/ps awwx|/bin/grep -v grep|/bin/grep mysqld|/bin/grep -w "$mypid "`; 
   $x = substr($proc,index($proc,"/"),length($proc));
   $y = grep(/--defaults-file=/,$x);
   if ( $y == 0) {
      if (-e '/etc/my.cnf') {
         $cf_mode = (stat('/etc/my.cnf'))[2];
         chomp($cf_mode);
      } else {
         $cf_mode=00000;
      }
   } else {
        @z = split (/ +/, $x);
        foreach (@z) {
           if (/^--defaults-file/) {
              chomp($_);
              my @fields = split(/=/,$_);       # get configuration file
              my $cffile = $fields[1];
              if (-e $cffile) {
                 $cf_mode = (stat($cffile))[2];
                 chomp($cf_mode);
              } else {
                 $cf_mode=00000;
              } 
           }
        }
   }
}
my $dir_mode = (stat($datadir))[2];       # mysql datadir permission mode
#my $uid = (stat($datadir))[4];
#my $gid = (stat($datadir))[5];
chomp($dir_mode);
printf ("em_result=%04o$DELIM%04o\n",$dir_mode & 07777, $cf_mode & 07777); 
exit 0;
